package com.jiang.common;

import com.jiang.exception.ParamException;
import com.jiang.utils.JsonResult;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 全局异常处理类
 */
@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(value = Exception.class)
    public JsonResult defaultErrorHandler(HttpServletRequest req, Exception e) {
        e.printStackTrace();
        return JsonResult.fail(e.getMessage());
    }

    @ExceptionHandler(value = ParamException.class)
    @ResponseBody
    public JsonResult paramErrorHandler(HttpServletRequest req, ParamException e) {
        return JsonResult.fail(e.getMessage());
    }
}
